package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName: WebMvcConfiguration
 * Package: com.atguigu.lease.web.admin.custom.config
 * Description:
 * 注册 自定义类型转换器 以及类型转换器工厂
 *
 * @Autor: xvyy
 * @Create: 2024/11/16 - 15:15
 * @Version: v1.0
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;
    @Value("${admin.auth.path-patterns.include}")  //跟配置文件中的属性进行绑定
    private String[] includePathPatterns;

    @Value("${admin.auth.path-patterns.exclude}")
    private String[] excludePathPatterns;

    //    @Autowired
//    private StringToItemTypeConverter stringToItemTypeConverter;
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

    /**
     * 将字符串转换为ItemType枚举类型
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        //todo 注册自定义类型转换器 : 用于将字符串转换为ItemType枚举类型
//        registry.addConverter(/*this.*/stringToItemTypeConverter);
        registry.addConverterFactory(stringToBaseEnumConverterFactory);
    }

    /**
     * 用于权限校验
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //todo 注册自定义拦截器 : 用于权限校验
/*        // todo 拦截器的拦截路径 /admin/** 排除 /admin/login/** 路径
        registry.addInterceptor(this.authenticationInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login/**");*/

        registry.addInterceptor(this.authenticationInterceptor)
                .addPathPatterns(includePathPatterns).excludePathPatterns(excludePathPatterns);
    }
}